<?php

namespace frontend\controllers;

use bc\controller\WxOAuthController;
use bc\model\ecs\UserModel;
use bc\model\source\StatisticsModel;
use bc\model\wx\WxchuserModel;
use backend\controllers\AgentController as Agent;
use yii\web\Session;
use bc\model\wc\AgentModel;
use bc\util\Sms;
use bc\model\wx\report\CodeModel;
use bc\ar\MActiveRecord;
use yii;

class AgentController extends WxOAuthController
{
	
	//登录界面
	public 	function actionLogin()
	{
		return $this->render('login');
	}

	//判断登录
	public function actionCheckout()
	{
		$mobile = $this->post('mobile');
		$userModel = new UserModel ();
		$userInfo = $userModel->getRow([
			'mobile'=>$mobile,
		]);
		$agentModel = new AgentModel();
		$agentInfo = $agentModel->getRow([
			'user_id'=>$userInfo['user_id'],
		]);
		if( $agentInfo ){
			$this->redirect('/agent/index?id='.$agentInfo['id']);
		}else{
			echo "<script>alert('您还不是个人注册用户');window.location.href='/agent/apply'</script>";
			die();
		}
	}

	//登录个人二维码界面
	public function actionIndex()
	{
		$session = new Session();
		$user = $session->get('shop_user');
		
		$agentModel = new AgentModel();
		
		$row = $agentModel->getRow(['user_id' => $user['user_id']]);
		
		if(!$row)
		{
			echo "<script>alert('您还不是个人注册用户');window.location.href='/agent/apply'</script>";
			exit;
		}
		
		if($row['reply_status'] == 1)
		{
			$this->redirect('/agent/success?type=1');
			exit;
		}
		
		if($row['reply_status'] == 3)
		{
			echo "<script>alert('您的审核未通过，请重新申请');window.location.href='/agent/apply'</script>";
			exit;
		}
		
		
		$StatisticsModel = new StatisticsModel();
		$statistics = $StatisticsModel->getRowById($row['scene_id']);
		
		$wxuserModel = new WxchuserModel();
		$nickname = $agentModel->getAgentInfo($user['user_id']);
		$wxInfo = $wxuserModel->getRow([
			'nickname'	=>	$nickname,
		]);
		
		// 获取生成二维码的地址
		$data['headimgurl'] = $wxInfo['headimgurl'];
		$data['statistics'] = $statistics;
		return $this->render('index', $data);
	}


	// apply页面
	public function actionApply()
	{
		$session = new Session();
		$user = $session->get('shop_user');
		$agentModel = new AgentModel();
		$row = $agentModel->getRow(['user_id' => $user['user_id']]);
		
		if($row)
			$this->redirect('/agent');
		
		return $this->render ( 'apply' );
	}

	//去主用户表中判断是否存在记录
	public function actionTest()
	{
		$session = new Session ();
		$shop_user = $session->get ( 'shop_user' );
		$shop_user_id = $shop_user ['user_id'];
		$wxid = $shop_user ['wxid'];
		$agentModel = new AgentModel ();
		$mobile = $this->post( 'mobile' );
		$userModel = new UserModel ();
		// 去数据表中查找记录
		$userInfo = $userModel->getRow ( [
				'user_id'  => $shop_user_id
		] );

		if ($userInfo)
		{
			// 当前用户和数据表中的用户记录一致
			if ($userInfo['mobile'] && $userInfo ['mobile'] == $mobile)
			{
				$this->redirect ( '/agent/check' );
			}else
			{
				// 不一致，清空原记录的手机号码，将手机号码添加到当前用户的那条记录
				$userModel->modify ( [
					'mobile' => $mobile,
					'wxid'	  => $wxid,
				], $userInfo['user_id'] );
				$this->redirect ( '/agent/check' );
			}
		}
		else
		{
			// 将手机号码添加到当前用户的记录中去
			$userModel->add ([
				'mobile' => $mobile,
				'wxid'   => $wxid
			]);
			$this->redirect ( '/agent/check' );
		}
		return $this->render ( '/agent/test' );
	}

	//根据状态判断是否可以再次申请
	public function actionCheck()
	{
		$session = new Session ();
		$shop_user = $session->get ( 'shop_user' );
		$shop_user_id = $shop_user ['user_id'];
		$agentModel = new AgentModel();
		$agentInfo = $agentModel->getRow([
			'user_id' => $shop_user_id,
		]);
		if( $agentInfo ){
			if( $agentModel->getAgentById( $agentInfo['user_id']) ){
				echo "<script>alert('请勿重复申请');window.location.href='/agent/apply';</script>";
				die();
			}
		}
		$this->redirect('/agent/info');
		return $this->render('/agent/check');
	}

	/**
	 * 校验验证码
	 */
	public function actionValidcode()
	{
		$msg ['error'] = 1;
		
		if ($this->getRequest ()->isAjax == true)
		{
			$mobile = $this->get ( 'mobile' );
			$code = $this->get ( 'code' );
			$params ['mobile'] = $mobile;
			
			$codeModel = new CodeModel ();
			$codeModel->sort = 'id DESC';
			$code_row = $codeModel->getList ( [ 
					'mobile' => $mobile,
					'code' => $code 
			] );
			$msg ['data'] = json_encode ( $code_row );
			if (! $code_row [0])
			{
				$msg ['msg'] = '请输入正确的验证码';
			}
			elseif ($code_row [0] ['create_time'] < time () - 60)
			{
				$msg ['msg'] = '验证码已过期';
			}
			else
			{
				$msg ['error'] = 0;
			}
		}
		else
		{
			$msg ['msg'] = '请求异常';
		}
		
		echo json_encode ( $msg );
		exit ();
	}
	
	/**
	 * ajax方式发送验证码
	 */
	public function actionSend()
	{
		$msg = [ ];
		
		if ($this->getRequest ()->isAjax == true)
		{
			$mobile = $this->get ( 'mobile' );
			$model = new \bc\model\wx\ReportModel ();
			$params ['mobile'] = $mobile;
			// 13651886355
			
			// 创建一个新的验证码
			$arr = [ ];
			
			for($i = 0; $i < 4; $i ++)
			{
				$rand = rand ( 0, 9 );
				$arr [] = $rand;
			}
			
			$code = implode ( '', $arr );
			$data ['code'] = $code;
			$data ['mobile'] = $mobile;
			$data ['create_time'] = time ();
			
			$codeModel = new CodeModel ();
			$codeModel->sort = 'id DESC';
			$code_row = $codeModel->getList ( [ 
					'mobile' => $mobile 
			] );
			$codeModel->add ( $data );
			
			// 发送短信验证码
			$sms_msg = '验证码' . $code . '，本次验证码60s内有效';
			
			$sms = new Sms ();
			$sms->setMobile ( $mobile );
			$sms->setText ( $sms_msg );
			$result = $sms->send ();
			$msg ['error'] = 0;
			$msg ['result'] = $result;
			$msg ['msg'] = '验证码已发送!';
		}
		else
		{
			$msg ['error'] = 1;
			$msg ['msg'] = '请求参数错误！';
		}
		
		echo json_encode ( $msg );
		exit ();
	}
	
	/**
	 * 完善信息页面
	 */
	public function actionInfo()
	{
		return $this->render ( '/agent/info' );
	}

	//申请信息入库
	public function actionInter()
	{
		$session = new Session ();
		$shop_user = $session->get ( 'shop_user' );
		$shop_user_id = $shop_user ['user_id'];
		$agentModel = new AgentModel ();
		$data ['user_id'] = $shop_user_id;
		$data ['name'] = $this->post ( 'name' );
		$data ['sex'] = $this->post ( 'sex' );
		$data ['create_time'] = time ();
		$agentModel->add ( $data );
		$this->redirect ( '/agent/success' );
	}
	
	// 申请成功提示页面
	public function actionSuccess()
	{
		return $this->render ( '/agent/success' );
	}
	

	/**
	 *
	 */
	protected function getApiUrl()
	{
		if(YII_ENV == 'prod')
			return 'http://wx.zhishimama.com/';
		else
			return 'http://test.wx.zhishimama.com/';

	}

}

